package com.csdj.t309.papermanage.util;

import java.security.MessageDigest;

public class Md5Utils {
        static char[] hex = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        //md5加密
        public String md5(String content){
            MessageDigest md5 = null;
            try{
                md5 = MessageDigest.getInstance("MD5");//申明使用MD5算法
                md5.update(content.getBytes());
            }catch(Exception e){
                e.printStackTrace();
            }
            return byte2str(md5.digest());
        }

        /**
         * 将字节数组转换成十六进制字符串
         * @param bytes
         * @return
         */
        private static String byte2str(byte []bytes){
            int len = bytes.length;
            StringBuffer result = new StringBuffer();
            for (int i = 0; i < len; i++) {
                byte byte0 = bytes[i];
                result.append(hex[byte0 >>> 4 & 0xf]);
                result.append(hex[byte0 & 0xf]);
            }
            return result.toString();
        }

}
